package com.tanhua.admin.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.tanhua.admin.service.AdminService;
import com.tanhua.model.db.Admin;
import com.tanhua.model.vo.AdminVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/system/users")
public class SystemController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    /**
     * 验证码
     * */

    @GetMapping("/verification")
    public void verification(String uuid, HttpServletResponse response) throws IOException {
        //生成验证码
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(299, 97);
        //获取验证码
        String code = lineCaptcha.getCode();
        System.out.println(code);
        //存入redis
        redisTemplate.opsForValue().set("CODE:"+uuid,code,5, TimeUnit.MINUTES);
        //响应验证码图片
        lineCaptcha.write(response.getOutputStream());
    }
    /**
     * 用户登入
     * */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map map){
        //账号
        String username = map.get("username").toString();
        //密码
        String password = map.get("password").toString();
        //验证码
        String verificationCode = map.get("verificationCode").toString();
        //uuid
        String uuid = map.get("uuid").toString();
        //登入
        Map retMap = adminService.login(username,password,verificationCode,uuid);
        return ResponseEntity.ok(retMap);
    }
    /**
     * 获取用户信息
     *
     * */
    @PostMapping("/profile")
    public ResponseEntity profile(){
       AdminVo vo = adminService.profile();
       return ResponseEntity.ok(vo);
    }
}
